package com.ruoyi.tb.lmt.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.fastjson.JSONArray;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.tb.lmt.domain.vo.TbContentsVo;
import com.ruoyi.tb.lmt.domain.vo.TbLmttypeVo;
import com.ruoyi.tb.lmt.domain.vo.TbRepairplanlistVo;
import com.ruoyi.tb.lmt.mapper.TbContentsMapper;
import com.ruoyi.tb.lmt.mapper.TbWorkFileMapper;
import com.ruoyi.tb.lmt.service.ITbLmttypeService;
import com.ruoyi.tb.lmt.service.ITbRepairplanlistService;
import com.ruoyi.tb.lmt.service.ITbWorkFileService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@SaIgnore
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xha-lmt/contents")
public class TbContentsController {
    @Autowired
    private TbContentsMapper tbContentsMapper;

    @Autowired
    private ITbRepairplanlistService tbRepairplanlistService;

    @Autowired
    private ITbLmttypeService tbLmttypeService;

    @Autowired
    private TbWorkFileMapper tbWorkFileMapper;

    @GetMapping("/list")
    public R list(TbContentsVo tbContentsVo) {
        return R.ok(tbContentsMapper.selectList(tbContentsVo));
    }

    /**
     * 根据检修计划id获取文件列表
     */
    @GetMapping("/get/{id}")
    public R get(@PathVariable("id") Long id) {
        TbRepairplanlistVo tbRepairplanlistVo = tbRepairplanlistService.queryById(id);
        if (tbRepairplanlistVo == null || StringUtils.isBlank(tbRepairplanlistVo.getLmtType())) {
            return R.fail("检修计划不存在");
        }
        TbLmttypeVo tbLmttypeVo = tbLmttypeService.selectByLmtType(tbRepairplanlistVo.getLmtType());
        if (tbLmttypeVo == null) {
            return R.fail("车型不存在");
        }
        Long contentId = null;
        switch (tbRepairplanlistVo.getRepairType()) {
            case "二级修":
                contentId = tbLmttypeVo.getTwoC();
                break;
            case "三级修":
                contentId = tbLmttypeVo.getThreeC();
                break;
            case "四级修":
                contentId = tbLmttypeVo.getFourC();
                break;
        }
        if (contentId == null) {
            return R.fail("当前车车型没有" + tbRepairplanlistVo.getRepairType() + "的报告目录");
        }
        TbContentsVo vo = tbContentsMapper.getById(contentId);
        if (vo == null) {
            return R.fail("当前车车型没有" + tbRepairplanlistVo.getRepairType() + "的报告目录");
        }
        String contents = vo.getContents();
        Gson gson = new Gson();
        List<Map<String, String>> contentsList = gson.fromJson(contents, new TypeToken<List<Map<String, String>>>() {
        }.getType());
        for (Map<String, String> param : contentsList) {
            //名称
            param.get("key");
            //所需数
           /* param.get("needNumber");
            //实际数
            param.get("actualNumber");*/
            param.put("needNumber","1");
            param.put("actualNumber", tbWorkFileMapper.countByLmtRepairIdAndName(id, param.get("key")));
        }
        vo.setContents("");
        vo.setContentsList(contentsList);
        return R.ok(vo);
    }
}
